package com.liu.io.file.excel.read;

import com.liu.io.file.excel.consts.FileConst;
import com.liu.io.file.excel.poi.CSVUtil;
import com.liu.io.file.excel.poi.HSSFWorkbookUtil;
import com.liu.io.file.excel.poi.XSSFWorkbookUtil;
import jxl.read.biff.BiffException;

import java.io.*;
import java.util.*;

/**
 * 读取EXCLE
 * @author liubo
 */
public class ExcelRead {
    /**
     * 读取excel的表，默认读取第一张表
     */
    private int[] sheets = {0};

    /**
     * 读取EXCEL
     *
     * @param file
     * @return
     * @throws BiffException
     * @throws IOException
     */
    public Map<String, Object> readExcel(File file) throws BiffException, IOException {
        Map<String, Object> data = new HashMap<String, Object>();
        String name = file.getName();

        if (name.endsWith(FileConst.EXCEL_TYPE_XLS)) {
            data = new HSSFWorkbookUtil().readXls(file, sheets);
        } else if (name.endsWith(FileConst.EXCEL_TYPE_XLSX)) {
            data = new XSSFWorkbookUtil().readXlsx(file, sheets);
        } else if (name.endsWith(FileConst.EXCEL_TYPE_CSV)) {
            data = new CSVUtil().readCsv(file, sheets);
        } else {

        }
        return data;
    }

    public void setSheets(int[] sheets) {
        this.sheets = sheets;
    }

    public int[] getSheets() {
        return sheets;
    }
}